package com.bilicraft.plugin.kexue.jsonapi;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import com.alecgorge.minecraft.jsonapi.JSONAPI;
import com.alecgorge.minecraft.jsonapi.api.APIMethodName;
import com.alecgorge.minecraft.jsonapi.api.JSONAPICallHandler;
/** handlers here*/
public class JSONAPIHandler implements JSONAPICallHandler{

	private static JSONAPIHandler instance;
	
	public static boolean isEnable = false;
	
	private Map<String, JaCmd> cmdTable = new HashMap<String, JaCmd>();
	
	private JSONAPIHandler(){
		cmdTable.put("getSpellBooks", new JaCmdGetSpellBooks());
		cmdTable.put("getHeroPlayer", new JaCmdGetHeroPlayer());
		cmdTable.put("drawPixel", new JaCmdDrawPixel());
		cmdTable.put("drawPixelAt", new JaCmdDrawPixelAt());
	}
	
	public static JSONAPIHandler getInstance(){
		if(instance == null){
			instance = new JSONAPIHandler();
		}
		return instance;
	}
	
	public static void enable(){
		Plugin checkplugin = Bukkit.getServer().getPluginManager().getPlugin("JSONAPI");
		if(checkplugin != null && checkplugin.isEnabled()) {
			JSONAPI jsonapi = (JSONAPI)checkplugin;
					
			jsonapi.registerAPICallHandler(getInstance());
			isEnable = true;
			
			Bukkit.getLogger().info("BiliCraft's JSONAPI handlers actived.");
		}
		else {
		}
	}
	
	@Override
	public Object handle(APIMethodName arg0, Object[] arg1) {
		JaCmd jacmd = cmdTable.get(arg0.toString());
		if(jacmd != null){
			return jacmd.exec(arg0, arg1);
		}
		return null;
	}

	@Override
	public boolean willHandle(APIMethodName arg0) {
		JaCmd jacmd = cmdTable.get(arg0.toString());
		if(jacmd != null){
			return true;
		}
		return false;
	}
}
